﻿using System;

namespace class4Homework
{
    public static class alarmTime
    {
        private static string alarmHour;
        private static string alarmMin;
        private static string alarmSecond;
        private static bool isAlarm=false;
        public static string AlarmHour
        {
            get
            {
                return alarmHour;
            }
            set
            {
                alarmHour = value;
            }
        }
        public static string AlarmMin
        {
            get
            {
                return alarmMin;
            }
            set
            {
                alarmMin = value;
            }
        }
        public static string AlarmSecond
        {
            get
            {
                return alarmSecond;
            }
            set
            {
                alarmSecond = value;
            }
        }
        public static bool IsAlarm
        {
            get
            {
                return isAlarm;
            }
            set
            {
                isAlarm = value;
            }
        }
        public static bool selfcheck()
        {
            int h = int.Parse(AlarmHour);
            int m = int.Parse(AlarmMin);
            int s = int.Parse(AlarmSecond);
            if(h>24||m>59||s>59)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


    }
    public class nowTime:EventArgs
    {
        public string hour;
        public string min;
        public string second;
    }
    public delegate void alarmWorking(object sender, nowTime e);
    class alarm
    {
        public event alarmWorking working;
        public void isWorking()
        {
            
                while (true)
                {
                    System.DateTime currentTime = new System.DateTime();
                    currentTime = System.DateTime.Now;
                    nowTime test1 = new nowTime();
                    test1.hour = currentTime.Hour.ToString();
                    test1.min = currentTime.Minute.ToString();
                    test1.second = currentTime.Second.ToString();
                    if (working != null)
                    {
                        working(this, test1);
                    }
                    System.Threading.Thread.Sleep(1000);
            }
        }
    }
    class Program
    {
        static void ticking(object sender, nowTime e)
        {
            Console.WriteLine("ticking!current Time is " + e.hour + ":" + e.min + ":" + e.second);
        }
        static void alarming(object sender,nowTime e)
        {
            if(alarmTime.AlarmHour.Equals(e.hour)&&alarmTime.AlarmMin.Equals(e.min)&&alarmTime.AlarmSecond.Equals(e.second))
            {
                alarmTime.IsAlarm = true;
            }
            if (alarmTime.IsAlarm)
            {
                Console.WriteLine("Alarming!Press Enter to stop!");
            }
            if(Console.KeyAvailable)
            {
                ConsoleKey InputKey1 = Console.ReadKey(true).Key;
                if(InputKey1==ConsoleKey.Enter)
                {
                    alarmTime.IsAlarm = false;
                }
                else if (InputKey1 == ConsoleKey.Backspace)
                {
                    Program.setAlarmtime();
                }
            }
        }
        
        public static void setAlarmtime()
        {
            while (true)
            {
                Console.WriteLine("设置闹钟时间");
                Console.WriteLine("设置Hour:");
                alarmTime.AlarmHour = Console.ReadLine().ToString();
                Console.WriteLine("设置Min:");
                alarmTime.AlarmMin = Console.ReadLine().ToString();
                Console.WriteLine("设置Second:");
                alarmTime.AlarmSecond = Console.ReadLine().ToString();
                if (alarmTime.selfcheck())
                {
                    Console.WriteLine("如果想重新设置闹钟时间，请按Backspace");
                    break;
                }
                else
                {
                    Console.WriteLine("设定闹钟时间有误，请重新输入");
                }
            }
        }
        
        static void Main(string[] args)
        {

            alarm ALARM = new alarm();
            ALARM.working += ticking;
            ALARM.working += alarming;
            try
            {
                setAlarmtime();
                ALARM.isWorking();
            }
            catch(Exception e)
            {
                Console.WriteLine("System Error!");
            }
        }
    }
}
